<?php
header("Content-type: application/vnd.ms-excel"); //文件內容為excel格式
header("Content-Disposition: attachment; filename="."output".date("YmdHis").".xls;"); //將PHP轉成下載的檔案指定名稱與副檔名.xls


// header("Content-Type: application/force-download");
// header("Content-Type: application/octet-stream");
// header("Content-Type: application/download");
// header("Content-Transfer-Encoding:utf-8");
// header("Pragma: no-cache");
// header('Content-Type: application/vnd.ms-excel');
// header('Cache-Control: max-age=0');
// header("Content-Disposition:filename=" . "_" . date("Ymd") . time() . ".xls");


$user_array = array();
foreach($users as $user){
	$user_array[$user->users_id] = $user->name;
}
$titles = array("序號", "客戶姓名", "特徵", "重要性", "國籍", "英文姓名", "性別", "客戶來源", "生日", "送禮", "身份證字號", "平常聯絡", "開始時間", "結束時間", "平常聯絡1", "開始時間1", "結束時間1", "平常聯絡2", "開始時間2", "結束時間2", "假日聯絡", "結束時間", "開始時間", "假日聯絡1", "開始時間1", "結束時間1", "假日聯絡2", "開始時間2", "結束時間2", "背景與特色", "行動電話", "簡訊發送", "行動電話1", "簡訊發送1", "行動電話2", "簡訊發送2", "住家電話", "住家電話1", "住家電話2", "公司電話", "公司電話1", "公司電話2", "分機", "分機1", "分機2", "電子郵件", "寄發EDM", "電子郵件1", "寄發EDM1", "電子郵件2", "寄發EDM2", "傳真", "傳真1", "傳真2", "住家地址", "大樓名稱", "住家寄發刊物", "住家地址1", "大樓名稱1", "住家寄發刊物1", "住家地址2", "大樓名稱2", "住家寄發刊物2", "公司名稱", "公司類型", "統一編號", "部門", "職稱", "類別", "公司地址", "大樓名稱", "公司寄發刊物", "公司網址", "公司名稱1", "公司類型1", "統一編號1", "部門1", "職稱1", "類別1", "公司地址1", "大樓名稱1", "公司寄發刊物1", "公司網址1", "公司名稱2", "公司類型2", "統一編號2", "部門2", "職稱2", "類別2", "公司地址2", "大樓名稱2", "公司寄發刊物2", "公司網址2", "接待人", "主管", "原經紀人", "經紀人");

//echo '';
//echo '';
foreach ($titles as $key => $name) {
	//echo '';
	echo $name;
	echo "\t";
}
echo "\n";


$user_ar = array();
foreach($users as $user){
    $user_ar[$user->users_id] = $user->name;
}

$i = 1;

foreach ($result as $ccc => $val) {
	//if(1){
		//echo '';
		
		
		echo  $i . "\t";
		echo  $val -> name . "\t";
		echo  $val -> recommended . "\t";

		if (isset($importance_of_customers[$val -> importance])) {
			echo   $importance_of_customers[$val -> importance] . "\t";
		} else {
			echo "\t";
		}

		//echo '' ;  
		//if(!is_numeric($val -> nationality)AND $val -> nationality!=0)
	        foreach($nationality as $key=>$value){
	            if($val -> nationality ==$key){
	                echo $value;
	            }
	        }
	        	        
	    echo "\t";
	        
	        
		echo  "'" . $val -> english_name . "\t";
		echo  "'";  
		if($val -> sex==1){
			echo '男';
		}else {
			echo '女';
			
		} 
		echo  "\t";

		if (isset($customer_source[$val -> customer_source])) {
			echo  $customer_source[$val -> customer_source] . "\t";
		} else {
			echo "\t";
		}

		echo  $val -> birthday . "\t";
		echo  $val -> gifts . "\t";

		echo  $val -> id_number . "\t";
		if (isset($contact_usual_holiday[$val -> usually_contact])) {
			echo  $contact_usual_holiday[$val -> usually_contact] . "\t";
		} else {
			echo "\t";
		}


		if($val -> usually_start_time!=0){
			echo  $val -> usually_start_time . "\t";
		}else{
			echo "\t";
		}
		echo  $val -> usually_end_time . "\t";

		if (isset($contact_usual_holiday[$val -> usually_contact1])) {
			echo  $contact_usual_holiday[$val -> usually_contact1] . "\t";
		} else {
			echo "\t";
		}

		if($val -> usually_start_time1!=0){
			echo  $val -> usually_start_time1 . "\t";
		}else{
			echo "\t";	
		}


		if($val -> usually_end_time1!=0){
			echo  $val -> usually_end_time1 . "\t";
		}else{
			echo "\t";	
		}

		

		if (isset($contact_usual_holiday[$val -> usually_contact2])) {
			echo  $contact_usual_holiday[$val -> usually_contact2] . "\t";
		} else {
			echo "\t";
		}
		
		
		if($val -> usually_end_time2!=0){
			echo  $val -> usually_end_time2 . "\t";
		}else{
			echo "\t";	
		}

		if($val -> usually_end_time2!=0){
			echo  $val -> usually_end_time2 . "\t";
		}else{
			echo "\t";	
		}	

		

		if (isset($contact_usual_holiday[$val -> holidays_contact])) {
			echo  $contact_usual_holiday[$val -> holidays_contact] . "\t";
		} else {
			echo "\t";
		}

		if($val -> holidays_start_time!=0){
			echo  $val -> holidays_start_time . "\t";
		}else{
			echo "\t";	
		}

		if($val -> holidays_end_time!=0){
			echo  $val -> holidays_end_time . "\t";
		}else{
			echo "\t";	
		}
		

		if (isset($contact_usual_holiday[$val -> holidays_contact1])) {
			echo  $contact_usual_holiday[$val -> holidays_contact1] . "\t";
		} else {
			echo "\t";
		}
		
		//echo '<td style="background-color:red;clolr:red;">??\t';
	    //echo '<td style="background-color:red;clolr:red;">??\t';

		if($val -> holidays_start_time1!=0){
			echo  $val -> holidays_start_time1 . "\t";
		}else{
			echo "\t";	
		}

		if($val -> holidays_end_time1!=0){
			echo  $val -> holidays_end_time1 . "\t";
		}else{
			echo "\t";	
		}


		
		

		if (isset($contact_usual_holiday[$val -> holidays_contact2])) {
			echo  $contact_usual_holiday[$val -> holidays_contact2] . "\t";
		} else {
			echo "\t";
		}

		if($val -> holidays_start_time2!=0){
			echo  $val -> holidays_start_time2 . "\t";
		}else{
			echo "\t";	
		}

		if($val -> holidays_end_time2!=0){
			echo  $val -> holidays_end_time2 . "\t";
		}else{
			echo "\t";	
		}

		
		echo  $val -> background_and_features . "\t";
	
		if($val -> mobile!=0){
			echo  $val -> mobile . "\t";
		}else{
			echo "\t";	
		}
		
		


		echo  $val -> send_sms . "\t";

		if($val -> mobile1!=0){
			echo  $val -> mobile1 . "\t";
		}else{
			echo "\t";	
		}


		echo  $val -> send_sms1 . "\t";

		if($val -> mobile2!=0){
			echo  $val -> mobile2 . "\t";
		}else{
			echo "\t";	
		}
		
		echo  $val -> send_sms2 . "\t";

		

		if($val -> home_phone!=0){
			echo  $val -> home_phone . "\t";
		}else{
			echo "\t";	
		}

		if($val -> home_phone1!=0){
			echo  $val -> home_phone1 . "\t";
		}else{
			echo "\t";	
		}

		if($val -> home_phone2!=0){
			echo  $val -> home_phone2 . "\t";
		}else{
			echo "\t";	
		}

		if($val -> company_phone!=0){
			echo  $val -> company_phone . "\t";
		}else{
			echo "\t";	
		}

		if($val -> company_phone1!=0){
			echo  $val -> company_phone1 . "\t";
		}else{
			echo "\t";	
		}

		if($val -> company_phone2!=0){
			echo  $val -> company_phone2 . "\t";
		}else{
			echo "\t";	
		}

		
		
		if($val -> company_phone_ext!=0){
			echo  $val -> company_phone_ext . "\t";
		}else{
			echo "\t";	
		}

		if($val -> company_phone_ext1!=0){
			echo  $val -> company_phone_ext1 . "\t";
		}else{
			echo "\t";	
		}

		if($val -> company_phone_ext2!=0){
			echo  $val -> company_phone_ext2 . "\t";
		}else{
			echo "\t";	
		}



		if($val -> email!=0){
			echo  $val -> email . "\t";
		}else{
			echo "\t";	
		}

		if($val -> send_email!=0){
			echo  $val -> send_email . "\t";
		}else{
			echo "\t";	
		}

		if($val -> email1!=0){
			echo  $val -> email1 . "\t";
		}else{
			echo "\t";	
		}

		if($val -> send_email1!=0){
			echo  $val -> send_email1 . "\t";
		}else{
			echo "\t";	
		}

		if($val -> email2!=0){
			echo  $val -> email2 . "\t";
		}else{
			echo "\t";	
		}

		if($val -> send_email2!=0){
			echo  $val -> send_email2 . "\t";
		}else{
			echo "\t";	
		}

		if($val -> fax!=0){
			echo  $val -> fax . "\t";
		}else{
			echo "\t";	
		}

		if($val -> fax1!=0){
			echo  $val -> fax1 . "\t";
		}else{
			echo "\t";	
		}

		if($val -> fax2!=0){
			echo  $val -> fax2 . "\t";
		}else{
			echo "\t";	
		}

		if($val -> home_address!=0){
			echo  $val -> home_address . "\t";
		}else{
			echo "\t";	
		}

		if (isset($building_name[$val -> home_building_name])) {
			echo  $building_name[$val -> home_building_name] . "\t";
		} else {
			echo "\t";
		}

		echo  $val -> home_send_publication . "\t";

		if($val -> home_address1!=0){
			echo  $val -> home_address1 . "\t";
		}else{
			echo "\t";	
		}
		

		if (isset($building_name[$val -> home_building_name1])) {
			echo  $building_name[$val -> home_building_name1] . "\t";
		} else {
			echo "\t";
		}
			
		echo  $val -> home_send_publication1 . "\t";

		if($val -> home_address2!=0){
			echo  $val -> home_address2 . "\t";
		}else{
			echo "\t";	
		}
		 

		if (isset($building_name[$val -> home_building_name2])) {
			echo  $building_name[$val -> home_building_name2] . "\t";
		} else {
			echo "\t";
		}

		echo  $val -> home_send_publication2 . "\t";
		

		if($val -> company_name!=0){
			echo  $val -> company_name . "\t";
		}else{
			echo "\t";	
		}

		if (isset($type_of_company[$val -> type_of_company])) {
			echo  $type_of_company[$val -> type_of_company] . "\t";
		} else {
			echo "\t";
		}

		if($val -> unicode!=0){
			echo  $val -> unicode . "\t";
		}else{
			echo "\t";	
		}

		if($val -> department!=0){
			echo  $val -> department . "\t";
		}else{
			echo "\t";	
		}

		 

		if (isset($title[$val -> title])) {
			echo  $title[$val -> title] . "\t";
		} else {
			echo "\t";
		}

		if (isset($customer_type[$val -> category])) {
			echo  $customer_type[$val -> category] . "\t";
		} else {
			echo "\t";
		}
		if($val -> company_address!=0){
			echo  $val -> company_address . "\t";
		}else{
			echo "\t";	
		}
	

		if (isset($building_name[$val -> company_building_name])) {
			echo  $building_name[$val -> company_building_name] . "\t";
		} else {
			echo "\t";
		}


	if($val -> company_send_publication!=0){
			echo  $val -> company_send_publication . "\t";
		}else{
			echo "\t";	
		}
	

		if($val -> company_website!=0){
			echo  $val -> company_website . "\t";
		}else{
			echo "\t";	
		}
	

		if($val -> company_name1!=0){
			echo  $val -> company_name1 . "\t";
		}else{
			echo "\t";	
		}
	
		

		if (isset($type_of_company[$val -> type_of_company1])) {
			echo  $type_of_company[$val -> type_of_company1] . "\t";
		} else {
			echo "\t";
		}

		if($val -> unicode1!=0){
			echo  $val -> unicode1 . "\t";
		}else{
			echo "\t";	
		}

		if($val -> department1!=0){
			echo  $val -> department1 . "\t";
		}else{
			echo "\t";	
		}

		

		if (isset($title[$val -> title1])) {
			echo  $title[$val -> title1] . "\t";
		} else {
			echo "\t";
		}

		if (isset($customer_type[$val -> category1])) {
			echo  $customer_type[$val -> category1] . "\t";
		} else {
			echo "\t";
		}

		
		if($val -> company_address1!=0){
			echo  $val -> company_address1 . "\t";
		}else{
			echo "\t";	
		}

		if (isset($building_name[$val -> company_building_name1])) {
			echo  $building_name[$val -> company_building_name1] . "\t";
		} else {
			echo "\t";
		}


		if($val -> company_send_publication1!=0){
			echo  $val -> company_send_publication1 . "\t";
		}else{
			echo "\t";	
		}

		if($val -> company_website1!=0){
			echo  $val -> company_website1 . "\t";
		}else{
			echo "\t";	
		}

		if($val -> company_name2!=0){
			echo  $val -> company_name2 . "\t";
		}else{
			echo "\t";	
		}

		 
		
		
		
		if (isset($type_of_company[$val -> type_of_company2])) {
			echo  $type_of_company[$val -> type_of_company2] . "\t";
		} else {
			echo "\t";
		}

		if($val -> unicode2!=0){
			echo  $val -> unicode2 . "\t";
		}else{
			echo "\t";	
		}

		if($val -> department2!=0){
			echo  $val -> department2 . "\t";
		}else{
			echo "\t";	
		}

		
		
		
		if (isset($title[$val -> title2])) {
			echo  $title[$val -> title2] . "\t";
		} else {
			echo "\t";
		}
		
		
		if (isset($customer_type[$val -> category2])) {
			echo  $customer_type[$val -> category2] . "\t";
		} else {
			echo "\t";
		}
		
		if($val -> company_address2!=0){
			echo  $val -> company_address2 . "\t";
		}else{
			echo "\t";	
		}

		 
		
		
		
		if (isset($building_name[$val -> company_building_name2])) {
			echo  $building_name[$val -> company_building_name2] . "\t";
		} else {
			echo "\t";
		}
		
		if($val -> company_send_publication2!=0){
			echo  $val -> company_send_publication2 . "\t";
		}else{
			echo "\t";	
		}
		if($val -> company_website2!=0){
			echo  $val -> company_website2 . "\t";
		}else{
			echo "\t";	
		}
		 
		
		
		if (isset($user_array[$val -> front_man])) {
			echo  $user_array[$val -> front_man] . "\t";
		} else {
			echo "\t";
		}
		
		
		if (isset($user_array[$val -> head])) {
			echo  $user_array[$val -> head] . "\t";
		} else {
			echo "\t";
		}
		
		
		
		if (isset($user_array[$val -> original_broker])) {
			echo  $user_array[$val -> original_broker] . "\t";
		} else {
			echo "\t";
		}
		
		
		
		
		
		//echo  $val -> broker . "\t";
		if (isset($user_array[$val -> broker])) {
			echo  $user_array[$val -> broker] . "\t";
		} else {
			echo "\t";
		}
		//echo  $val -> ctime . "\t";
		echo "\n";
		
	
	$i++;
}

echo '';
?>